package o9getbfroma;

/**
 * @author shkstart
 * @create 2022-01-04-18:59
 */
public class StrStr {


    public static void main(String[] args) {
        int i = strStr("mississippi", "issip");
        System.out.println(i);
    }


    public static int strStr(String haystack, String needle) {
        int lengthA = haystack.length();
        int lengthB = needle.length();
        int sign = 0;

        if (lengthB == 0) {
            return 0;
        }

        for (int i = 0; i < lengthA - lengthB + 1; i++) {
            if (haystack.charAt(i) == needle.charAt(sign)) {
                sign++;
                for (int j = 0; j < lengthB; j++) {
                    if (haystack.charAt(i + j) != needle.charAt(j)) {
                        if (j == lengthB - 1) {
                            sign--;
                        }
                        break;
                    } else {
                        if (j == lengthB - 1) {
                            return i;
                        }
                    }
                }
            }
        }

        return -1;

    }

}
